new Vue({
   el: "#app",
   data:{
       totalMoney:0,
       productList:[],
       checkAllFlag:false,
       delFlag:false,
       curProduct:''
   },
    filters:{
        formatMoney:function (value) {
            return "￥ "+value.toFixed(2);
        }
    },
    mounted:function(){
        this.$nextTick(function () {
            this.cartVeiw();
        });
    },
    methods:{
       cartVeiw: function () {
           let _this = this;
           this.$http.get("data/cartData.json").then(res=>{
               this.productList = res.body.result.list;
               // this.totalMoney = res.body.result.totalMoney;
           });
       },
       changeMoney: function (product,way) {
           if(way>0){
               product.productQuantity++;
           }else{
               if(product.productQuantity>1){
                   product.productQuantity--;
               }
           }
           this.calcTotalPrice();
       },
       selectedProduct:function (item) {
            if(typeof item.checked == "undefined"){
                // Vue.set(item,"checked",true);   //全局注册
                this.$set(item,"checked",true);     //局部注册
            }else{
                item.checked = !item.checked;
            }
            this.calcTotalPrice();
       },
        checkAll:function (flag) {
            var _this = this;
            this.checkAllFlag = flag;

            this.productList.forEach(function (item,index) {
                if(typeof item.checked == "undefined"){
                    // Vue.set(item,"checked",true);   //全局注册
                    _this.$set(item,"checked",_this.checkAllFlag);     //局部注册
                }else{
                    item.checked = _this.checkAllFlag;
                }
            });
            this.calcTotalPrice();
        },
        calcTotalPrice:function () {
            var _this = this;
            this.totalMoney = 0;
            this.productList.forEach(function (item,index) {
                if(item.checked){
                    _this.totalMoney += item.productPrice*item.productQuantity;
                }
            });
        },
        delConfirm:function (item) {
            this.delFlag = true;
            this.curProduct = item;
        },
        delProduct:function () {
           var index = this.productList.indexOf(this.curProduct);
           this.productList.splice(index,1);
           this.delFlag = false;
        }
    }
});
Vue.filter("money",function (value,type) {
    return "￥ "+value.toFixed(2)+type;
})